<template>
    <div id="personalAddressBookManage" class="center_div">
        <div class="searchForm searchFormDataManageBtn22px" style="margin-top: 1%;margin-left: 1%;">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="终端名称">
                    <el-input v-model="conName"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="medium" @click="querySearch" class="dataManageBtn22BtnpPx">查询
                    </el-button>
                    <el-button type="primary" size="medium" @click="add" class="dataManageBtn22BtnpPx">添加</el-button>
                    <el-button type="primary" size="medium" @click="addSelfGroup" class="dataManageBtn22BtnpPx">
                        添加自定义群组
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="table-wrap dataManage18px" style="margin-top: -1%">
            <el-table
                    :data="tableData"
                    style="width:100%;text-align: center;"
                    border
                    :header-cell-style="headerCellClassName"
                    cell-class-name="dataManage18px"
            >
                <el-table-column align="center" v-for="title in tableTitle" :prop="title.prop" :label="title.label"
                                 :show-overflow-tooltip="title.overflowTooltip">
                </el-table-column>
                <template v-if="buttons && buttons.length!==0">
                    <el-table-column
                            class="align-right"
                            header-align="center"
                            width="500px"
                            label="操作"
                            align="center">
                        <template slot-scope="scope">
                            <el-button v-for="button in buttons"
                                       @click="operate(scope.row,button.href)" :type="button.type"
                                       class="el-button el-button--text  dataManageBtn22px">{{button.label}}
                            </el-button>
                            <el-button v-if="scope.row.type ==4" @click="handleSelfGroupMg(scope.row)"
                                       class="el-button el-button--text  dataManageBtn22px">成员管理
                            </el-button>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
            <el-pagination
                    class="dataManage18px"
                    layout="total,prev, pager, next"
                    background
                    @current-change="changePageNum"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    :total="total">
            </el-pagination>
        </div>
        <div>
            <el-dialog v-if="loadTree" width="70%" :title="title" :visible.sync="showBox" @close="manageDialogClose"
                       append-to-body>
                <div class="groupMgStyleBigBoxLeft">
                    <div style="height:99%;width:99%;margin-top:1%;background:white;overflow:auto">
                        <el-tree check-strictly
                                 :load="loadFleetAll"
                                 show-checkbox
                                 highlight-current
                                 lazy
                                 node-key="id"
                                 :props="defaultProps"
                                 :filter-node-method="filterNode"
                                 ref="tree"
                                 @check-change="handleCheckChange">
                            <span class="custom-tree-node dataManage18px" slot-scope="{ node, data }">
                                <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===2"><img src="@/assets/images/mapicon/tree_group.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===3"><img src="@/assets/images/mapicon/tree_sub.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                 <span v-if="data.type===4"><img src="@/assets/images/mapicon/zuoxiUser.png"
                                                                 class="treeLableImg">{{ node.label }}</span>
                            </span>
                        </el-tree>
                    </div>
                </div>
                <div class="groupMgStyleBigBox">
                    <el-table
                            :data="getALLMemberTableData"
                            style="width:100%;text-align: center;"
                            border
                            class="groupMgTableClass"
                            cell-class-name="memberMgdataManageText"
                            :header-cell-style="headerCellClassName">
                        <el-table-column align="center" v-for="title in memberTableDataTableTitle" :prop="title.prop"
                                         :label="title.label" :show-overflow-tooltip="title.overflowTooltip">
                        </el-table-column>
                        <template>
                            <el-table-column
                                    class="align-right"
                                    width="230px"
                                    align="center"
                                    label="操作">
                                <template slot-scope="scope">
                                    <!--<span><img src="@/assets/images/newIcon/sortUp.png"
                                               class="groupMgStyle"
                                               @click="sortUpUp(index)">
                                    </span>-->
                                    <span>
                                        <el-button @click="deleteMember(scope.row.conId)" type="text"
                                                   class="el-button el-button--text  memberMgdataManageBtn">删除</el-button></span>
                                </template>
                            </el-table-column>
                        </template>
                    </el-table>
                    <el-pagination
                            v-if="false"
                            class="dataManage18px"
                            layout="total,prev, pager, next"
                            background
                            @current-change="changesAddPageNum"
                            :current-page="addPage.pageNum"
                            :page-size="addPage.pageSize"
                            :total="memberTableData.length">
                    </el-pagination>
                </div>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="showBox = false" size="medium" class="dataManageBtn22BtnpPx">取 消</el-button>
                    <el-button type="primary" @click="addPersonalAddressBook" size="medium"
                               class="dataManageBtn22BtnpPx">确 定
                    </el-button>
                </div>
            </el-dialog>
        </div>
        <!--添加自定义群组-->
        <div v-if="false">
            <el-dialog v-if="false" width="70%" title="添加自定义群组" :visible.sync="addSelfGroupShowBox"
                       @close="manageDialogClose" append-to-body>
                <div class="groupMgStyleBigBoxLeft">
                    <div style="height:99%;width:99%;margin-top:1%;background:white;overflow:auto;z-index: 999!important;">
                        <el-tree check-strictly
                                 :load="addSelfGrouploadFleetAll"
                                 show-checkbox
                                 highlight-current
                                 lazy
                                 node-key="id"
                                 :props="defaultProps"
                                 :filter-node-method="filterNode"
                                 ref="SelfGroupTree"
                                 @check-change="addSelfGrouphandleCheckChange">
                            <span class="custom-tree-node dataManage18px" slot-scope="{ node, data }">
                                <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===2"><img src="@/assets/images/mapicon/tree_group.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===3"><img src="@/assets/images/mapicon/tree_sub.png"
                                                                class="treeLableImg">{{ node.label }}</span>

                            </span>
                        </el-tree>
                    </div>

                </div>
                <div class="groupMgStyleBigBox">
                    <el-table
                            :data="selfGroupMemberTableData"
                            style="width:100%;text-align: center;"
                            border
                            class="groupMgTableClass"
                            cell-class-name="memberMgdataManageText"
                            :header-cell-style="headerCellClassName">
                        <el-table-column align="center" v-for="title in selfGroupmemberTableDataTableTitle"
                                         :prop="title.prop"
                                         :label="title.label" :show-overflow-tooltip="title.overflowTooltip">
                        </el-table-column>
                        <template>
                            <el-table-column
                                    class="align-right"
                                    width="230px"
                                    align="center"
                                    label="操作">
                                <template slot-scope="scope">
                                    <span>
                                        <el-button @click="addSelfGroupdeleteMember(scope.row.conId)" type="text"
                                                   class="el-button el-button--text  memberMgdataManageBtn">删除</el-button></span>
                                </template>
                            </el-table-column>
                        </template>
                    </el-table>
                    <el-pagination
                            v-if="false"
                            class="dataManage18px"
                            layout="total,prev, pager, next"
                            background
                            @current-change="changesAddPageNum"
                            :current-page="addPage.pageNum"
                            :page-size="addPage.pageSize"
                            :total="memberTableData.length">
                    </el-pagination>

                </div>

                <div class="groupFromBox">
                    <el-row>
                        <el-col :span="12">
                            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="200px"
                                     class="demo-ruleForm">
                                <el-form-item label="自定义组名称" prop="name">
                                    <el-input maxlength="22"
                                              v-model="ruleForm.name" placeholder="请输入自定义组名称"></el-input>
                                </el-form-item>
                            </el-form>
                        </el-col>
                        <el-col :span="12"></el-col>
                    </el-row>
                </div>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addSelfGroupShowBox = false" size="medium" class="dataManageBtn22BtnpPx">取 消
                    </el-button>
                    <el-button type="primary" @click="addSelfGroupConfire" size="medium"
                               class="dataManageBtn22BtnpPx">确 定
                    </el-button>
                </div>
            </el-dialog>
        </div>
        <!--添加自定义群组-->
        <div>
            <el-dialog title="添加自定义群组" :visible.sync="addSelfGroupShowBox" width="40%" append-to-body
                       @close="closeBoxaddSelfGroupShowBox">
                <el-form ref="form" :model="form" :rules="rules" label-width="140px">
                    <el-row>
                        <el-col>
                            <el-form-item label="编组名称" prop="name">
                                <el-input v-model="form.name"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row v-if="false">
                        <el-form-item label="描述">
                            <el-input v-model="form.remark" type="textarea"></el-input>
                        </el-form-item>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="closeBoxaddSelfGroupShowBox" class="dataManageBtn22BtnpPx">取 消</el-button>
                    <el-button type="primary" @click="addSelfGroupMember('form')" class="dataManageBtn22BtnpPx">添 加
                    </el-button>
                </div>
            </el-dialog>
        </div>
        <div>
            <el-dialog title="详情" width="40%" :visible.sync="showDetails" append-to-body>
                <div class="dataManage18px">名称：{{DetailsDatas.name}}</div>
                <div class="dataManage18px">分机号：{{DetailsDatas.extnum}}</div>
                <div class="dataManage18px">类型：{{DetailsDatas.typeName}}</div>
                <span slot="footer" class="dialog-footer">
    <el-button @click="showDetails = false" class="dataManageBtn22BtnpPx">关 闭</el-button>
                    <!--    <el-button type="primary" @click="showDetails = false">确 定</el-button>-->
  </span>
            </el-dialog>
        </div>

        <!--自定义组成员管理-->
        <div>
            <el-dialog v-if="loadTree" title="自定义组成员管理" width="70%" :visible.sync="showManageBox" @close="manageDialogClose"
                       append-to-body>
                <div>
                    <div class="groupMgStyleBigBoxLeft">
                        <div style="height:99%;width:99%;margin-top:1%;background:white;overflow:auto">
                            <el-tree check-strictly
                                     :load="ManageBoxloadFleetAll"
                                     show-checkbox
                                     highlight-current
                                     lazy
                                     node-key="id"
                                     :props="defaultProps"
                                     :filter-node-method="filterNode"
                                     ref="groupTree"
                                     @check-change="showManageBoxCheckChange">
                            <span class="custom-tree-node dataManage18px" slot-scope="{ node, data }">
                                <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===2"><img src="@/assets/images/mapicon/tree_group.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===3"><img src="@/assets/images/mapicon/tree_sub.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                 <span v-if="data.type===4"><img src="@/assets/images/mapicon/zuoxiUser.png"
                                                                 class="treeLableImg">{{ node.label }}</span>
                            </span>
                            </el-tree>
                        </div>
                    </div>
                    <div class="groupMgStyleBigBox">
                        <el-table
                                :data="selfGroupMemberTableData"
                                style="width:100%;text-align: center;"
                                border
                                class="groupMgTableClass"
                                cell-class-name="memberMgdataManageText"
                                :header-cell-style="headerCellClassName">
                            <el-table-column align="center" v-for="title in selfGroupmemberTableDataTableTitle"
                                             :prop="title.prop" :label="title.label"
                                             :show-overflow-tooltip="title.overflowTooltip">
                            </el-table-column>
                            <template>
                                <el-table-column
                                        class="align-right"
                                        width="230px"
                                        align="center"
                                        label="操作">
                                    <template slot-scope="scope">
                                        <el-button @click="showManageBoxDeleteMember(scope.row.extnumber)" type="text"
                                                   class="el-button el-button--text  memberMgdataManageBtn">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </template>
                        </el-table>
                        <el-pagination
                                v-if="false"
                                class="dataManage18px"
                                layout="total,prev, pager, next"
                                background
                                @current-change="changesAddPageNum"
                                :current-page="addPage.pageNum"
                                :page-size="addPage.pageSize"
                                :total="memberTableData.length">
                        </el-pagination>
                    </div>
                </div>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="showManageBox = false" class="dataManageBtn22BtnpPx">取 消</el-button>
                    <el-button type="primary" @click="handleAddCustomizeGrpMem" class="dataManageBtn22BtnpPx">确 定
                    </el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
  import PersonalAddressBookManage from '@/api/dataManages/PersonalAddressBookManage';
  export default {
    name: 'PersonalAddressBookManage',
    props: {
      FleetId: Number,
      userId: Number,
      UserAccount: String,
    },
    data() {
      return {
        conName: '',
        tableTitle: [
          {label: '名称', prop: 'name', overflowTooltip: true},
          {label: '分机号', prop: 'extnum', overflowTooltip: true},
          {label: '类型', prop: 'zhognduanType', overflowTooltip: true},
          {label: '备注', prop: 'comment', overflowTooltip: true},
        ],
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 8,
        addPage: {
          total: 0,
          pageNum: 1,
          pageSize: 4,
        },
        // buttons: [{label: '编辑', href: 'update'}, {label: '删除', href: 'delete'}],
        buttons: [{label: '详情', href: 'details'}, {label: '删除', href: 'delete'}],
        showBox: false,
        addSelfGroupShowBox: false,
        rules: {
          name: [
            {required: true, message: '请输入编组名称', trigger: 'blur'},
            {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'},
          ],
        },
        ruleForm: {
          name: '',
        },

        showDetails: false,
        DetailsDatas: {
          name: '',
          extnum: '',
          typeName: '',
        },
        memberTableDataTableTitle: [
          {label: '终端名称', prop: 'name', overflowTooltip: true},
          {label: '号码', prop: 'extnum', overflowTooltip: true},
        ],
        title: '',
        form: {
          id: '',
          conId: '',
          conName: '',
          type: '',
        },
        typeOptions: [{id: 0, name: '人员'}, {id: 1, name: '对讲组'}],
        conOptions: [],
        defaultProps: {
          label: 'name',
          isLeaf: 'isLeaf',
        },
        loadTree: true,
        showManageBox: false,
        selfMemberTableData: [],
        selfGroupId: '',
        memberTableData: [],
        getALLMemberTableData: [],
        selfGroupMemberTableData: [],//自定义组成员数据
        selfGroupmemberTableDataTableTitle: [
          {label: '终端名称', prop: 'name', overflowTooltip: true},
          {label: '号码', prop: 'number', overflowTooltip: true},
        ],
      };
    },
    created() {
      this.changeDmgPage();
    },
    mounted: function() {
      this.query();
    },
    computed: {
      addlimitData() {
        let data = [...this.memberTableData];
        // return this.memberTableData;
        return data;
      },
      dataWithAddPage() {
        const data = this.addlimitData;
        const start = this.addPage.pageNum * this.addPage.pageSize - this.addPage.pageSize;
        const end = start + this.addPage.pageSize;
        return [...data].slice(start, end);
      },
    },
    methods: {
      querySearch() {
        this.query(1);
        this.pageNum = 1;
      },
      changeDmgPage() {
        let w = window.screen.width;
        let h = window.screen.height;
        if (w == 1920) {
          if (h == 1080) {
          } else {
            this.pageSize = 11;
          }
        }
      },
      //表头样式
      headerCellClassName() {
        return 'font-size:18px;color:#515A6E;padding:10px 0px 10px 0px !important;background:#F5F5F6;border:0.05px solid #EBEBEB';
      },
      //表格样式
      cellStyleClassName() {
        // return 'font-size:13px;text-align:center;color:#606266;padding:5px 0px 5px 0px !important;border:0.05px solid #EBEBEB';
      },
      timeFormat(time) {
        var date, Y, M, D, h, m, s;
        date = new Date(time);
        Y = date.getFullYear() + '-';
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + '日 ';
        h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + h + m + s;
      },
      handleCheckChange(data, checked, indeterminate) {
        console.log(data);
        if (checked) {
          var nodeObj = {};
          nodeObj.conId = data.id;
          if (data.type === 2) {
            nodeObj.type = 1;
          } else if (data.type === 4) {
            nodeObj.type = 3;
          } else {
            nodeObj.type = 0;
          }
          nodeObj.conName = data.name;
          nodeObj.number = data.number;
          nodeObj.name = data.name;
          nodeObj.extnum = data.number;
          var length = false;
          for (var i = 0; i < this.getALLMemberTableData.length; i++) {
            if (data.id === this.getALLMemberTableData[i].conId) {
              length = true;
            }
          }
          if (!length) {
            this.getALLMemberTableData.push(nodeObj);
          }
        } else if (!checked) {
          for (var i = 0; i < this.getALLMemberTableData.length; i++) {
            if (data.id === this.getALLMemberTableData[i].conId) {
              this.getALLMemberTableData.splice(i, 1);
            }
          }
        }
      },
      showManageBoxCheckChange(data, checked, indeterminate) {
        console.log(data);
        if (checked) {
          var nodeObj = {};
          nodeObj.subId = data.id;
          if (data.type === 2) {
            nodeObj.type = 1;
          } else if (data.type === 3) {
            nodeObj.type = 0;
          } else if (data.type === 4) {
            nodeObj.type = 1;
          } else {
            nodeObj.type = 0;
          }
          nodeObj.name = data.name;
          nodeObj.extnumber = data.number;
          nodeObj.number = data.number;

          /*var length = false;
          for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
            if (data.extnumber === this.selfGroupMemberTableData[i].extnumber) {
              length = true;
            }
          }
          if (!length) {
            this.selfGroupMemberTableData.push(nodeObj);
          }
*/
          function isHasObj(arr, val) {
            var flag = false; // true为有 false为没有
            for (var i = 0; i < arr.length; i++) {
              if (JSON.stringify(arr[i]).indexOf(JSON.stringify(val)) != -1) {
                flag = true;
              }
            }
            return flag;
          }
          var keyIndex = isHasObj(this.selfGroupMemberTableData, nodeObj.number);
          if (keyIndex) {
            return false;
          } else {
            this.selfGroupMemberTableData.push(nodeObj);
          }
        } else if (!checked) {
          for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
            if (data.extnumber === this.selfGroupMemberTableData[i].extnumber) {
              this.selfGroupMemberTableData.splice(i, 1);
            }
          }
        }
      },
      showManageBoxDeleteMember: function(id) {
        console.log(this.selfGroupMemberTableData);
        for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
          if (id === this.selfGroupMemberTableData[i].extnumber) {
            this.selfGroupMemberTableData.splice(i, 1);
            var node = this.$refs.groupTree.getNode(id);
            if (node !== null && node !== undefined) {
              node.checked = false;
            }

          }
        }
      },deleteMember: function(id) {
        for (var i = 0; i < this.getALLMemberTableData.length; i++) {
          if (id === this.getALLMemberTableData[i].conId) {
            this.getALLMemberTableData.splice(i, 1);
            var node = this.$refs.tree.getNode(id);
            if (node !== null && node !== undefined) {
              node.checked = false;
            }

          }
        }
      },
      formClear: function() {
        this.form.id = '';
        this.form.conId = '';
        this.form.type = '';
      },
      manageDialogClose() {
        this.loadTree = false;
        setTimeout(() => {
          this.loadTree = true;
        }, 100);
        this.selfMemberTableData = [];
      },
      loadFleetAll(node, resolve) {
        if (node.data) {
          this.getChild(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => {
            this.getChild(this.FleetId, 0, resolve);
          }, 0);
        }
      },
      getChild(id, type, resolve) {
        var jsonData = {
          parentId: id,
          type: type,
        };
        PersonalAddressBookManage.getFleetAndGroupAndSub(jsonData).then(res => {
          if (res.success) {
            console.log('获取数据成功');
            var childNodes = [];
            if (res.obj && res.obj.length > 0) {
              for (var i = 0; i < res.obj.length; i++) {
                if (res.obj[i].type === 2 || res.obj[i].type === 3 || res.obj[i].type === 4) {
                  res.obj[i].isLeaf = true;
                } else {
                  res.obj[i].disabled = true;

                }
              }
              childNodes = res.obj;
            } else {
              var node = this.$refs.tree.getNode(id);
              node.loading = false;
              return;
            }
            resolve(childNodes);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        setTimeout(() => {
          for (let i = 0; i < this.getALLMemberTableData.length; i++) {
            var node = this.$refs.tree.getNode(this.getALLMemberTableData[i].conId);
            console.log(node);
            if (node != null && node.checked === false) {
              node.checked = true;
            }
          }
        }, 200);
        /*     this.$nextTick( ()=> {
               console.log(this.memberTableData);
               for(let i=0;i<this.memberTableData.length;i++){
                 // var node = this.$refs.tree.getNode(this.memberTableData[i].conId);
                 var node = this.$refs.tree.getNode(this.memberTableData[i].subId);
                 console.log(node);
                 if(node!=null && node.checked === false){
                   node.checked = true;
                 }
               }
             })*/
      },

      ManageBoxloadFleetAll(node, resolve) {
        if (node.data) {
          this.ManageBoxgetChild(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => {
            this.ManageBoxgetChild(this.FleetId, 0, resolve);
          }, 0);
        }
      },
      ManageBoxgetChild(id, type, resolve) {
        var jsonData = {
          parentId: id,
          type: type,
        };
        PersonalAddressBookManage.getFleetAndGroupAndSub(jsonData).then(res => {
          if (res.success) {
            console.log('获取数据成功');
            var childNodes = [];
            if (res.obj && res.obj.length > 0) {
              for (var i = 0; i < res.obj.length; i++) {
                if (res.obj[i].type === 2 || res.obj[i].type === 3 || res.obj[i].type === 4) {
                  res.obj[i].isLeaf = true;
                } else {
                  res.obj[i].disabled = true;

                }
              }
              childNodes = res.obj;
            } else {
              var node = this.$refs.tree.getNode(id);
              node.loading = false;
              return;
            }
            resolve(childNodes);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        setTimeout(() => {
          if(this.selfGroupMemberTableData.length>0){
            for (let i = 0,len = this.selfGroupMemberTableData.length; i < len; i++) {
              var node = this.$refs.groupTree.getNode(this.selfGroupMemberTableData[i].subId);
              console.log(node);
              if (node != null && node.checked === false) {
                node.checked = true;
              }
            }
          }

        }, 200);
      },
      filterNode: function(value, data) {
        if (!value)
          return true;
        else
          return data.name.indexOf(value) !== -1;
      },
      getConOptions(type) {
        if (type === 0) {
          PersonalAddressBookManage.getConOptions({type: type, fleetId: this.FleetId}).then(res => {
            var data = res;
            if (res.success) {
              this.conOptions = data.obj.list;
            } else {
              this.$message(res.msg);
            }

          }).catch(err => {
            // 异常情况
            console.log(err);
          });
        } else {
          PersonalAddressBookManage.getConOptions({type: type, fleetId: this.FleetId}).then(res => {
            var data = res;
            if (res.success) {
              this.conOptions = data.obj.list;
            } else {
              this.$message(res.msg);
            }

          }).catch(err => {
            // 异常情况
            console.log(err);
          });
        }
      },
      query(pageNum) {
        var data = {
          conName: this.conName || '',
          userId: this.UserAccount,
          pageNum: pageNum || this.pageNum,
          pageSize: this.pageSize,
        };
        PersonalAddressBookManage.getPersonalAddressBook(data).then(res => {
          if (res.success) {
            // 子组件调用父组件方法，并传递参数
            this.$emit('ChildgetHomepageMemberDatas');
            for (var i = 0; i < res.obj.list.length; i++) {
              res.obj.list[i].typeName = (res.obj.list[i].type === 0) ? '人员' : '对讲组';
            }
            let stateAllSubTypeDatas = this.$store.getters.stateAllSubType;
            console.log(stateAllSubTypeDatas);
            for (let t = 0; t < stateAllSubTypeDatas.length; t++) {
              for (var i = 0; i < res.obj.list.length; i++) {
                if (stateAllSubTypeDatas[t].ID == res.obj.list[i].subTypeId) {
                  res.obj.list[i]['zhognduanType'] = stateAllSubTypeDatas[t].name;
                }
                if (res.obj.list[i].type == 1) {
                  res.obj.list[i]['zhognduanType'] = '群组';
                } if (res.obj.list[i].type == 2) {
                  res.obj.list[i]['zhognduanType'] = '未知号码';
                  res.obj.list[i].extnum = res.obj.list[i].number;
                }
                if (res.obj.list[i].type == 4) {
                  res.obj.list[i]['zhognduanType'] = '自定义组';
                }if (res.obj.list[i].type == 3) {
                  res.obj.list[i]['zhognduanType'] = '坐席';
                }
              }
            }
            this.tableData = res.obj.list;
            this.memberTableData = res.obj.list;
            this.total = res.obj.total;
          } else {
            this.$message('');
          }
          this.conName = '';

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      /*获取所有终端数据，不分页*/
      allQuery(pageNum = 1) {
        var data = {
          userId: this.UserAccount,
          pageNum: 1,
          pageSize: 100000,
        };
        PersonalAddressBookManage.getPersonalAddressBook(data).then(res => {
          if (res.success) {
            this.getALLMemberTableData = res.obj.list;
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      add: function() {
        this.allQuery();
        this.showBox = true;
        this.title = '添加';
        // this.getMemberTableData();
      },
      /**
       * 添加自定义群组
       */
      addSelfGroup() {
        this.addSelfGroupShowBox = true;
      },
      /**
       * 添加自定义组确认事件
       */
      addSelfGroupConfire() {
        //TODO 添加自定义组确认事件

      },
      /**
       * 自定义组左侧树  过滤掉组
       **/
      addSelfGrouploadFleetAll(node, resolve) {
        if (node.data) {
          this.addSelfGroupgetChild(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => {
            this.addSelfGroupgetChild(this.FleetId, 0, resolve);
          }, 0);
        }
      },
      addSelfGroupgetChild(id, type, resolve) {
        var jsonData = {
          parentId: id,
          type: type,
        };
        PersonalAddressBookManage.getFleetAndGroupAndSub(jsonData).then(res => {
          if (res.success) {
            console.log('获取数据成功');
            var childNodes = [];
            if (res.obj && res.obj.length > 0) {
              for (var i = 0; i < res.obj.length; i++) {
                if (res.obj[i].type === 2 || res.obj[i].type === 3) {
                  res.obj[i].isLeaf = true;
                } else {
                  res.obj[i].disabled = true;

                }
              }
              childNodes = res.obj;
            } else {
              var node = this.$refs.tree.getNode(id);
              node.loading = false;
              return;
            }
            resolve(childNodes);
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        setTimeout(() => {
          console.log(this.memberTableData);
          for (let i = 0; i < this.memberTableData.length; i++) {
            var node = this.$refs.tree.getNode(this.memberTableData[i].conId);
            console.log(node);
            if (node != null && node.checked === false) {
              node.checked = true;
            }
          }
        }, 200);
        /*     this.$nextTick( ()=> {
               console.log(this.memberTableData);
               for(let i=0;i<this.memberTableData.length;i++){
                 // var node = this.$refs.tree.getNode(this.memberTableData[i].conId);
                 var node = this.$refs.tree.getNode(this.memberTableData[i].subId);
                 console.log(node);
                 if(node!=null && node.checked === false){
                   node.checked = true;
                 }
               }
             })*/
      },

      //点击树选择
      addSelfGrouphandleCheckChange(data, checked, indeterminate) {
        console.log(data);
        if (checked) {
          var nodeObj = {};
          nodeObj.conId = data.id;
          if (data.type === 2) {
            nodeObj.type = 1;
          } else {
            nodeObj.type = 0;
          }
          nodeObj.conName = data.name;
          nodeObj.number = data.number;
          var length = false;
          for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
            if (data.id === this.selfGroupMemberTableData[i].conId) {
              length = true;
            }
          }
          if (!length) {
            this.selfGroupMemberTableData.push(nodeObj);
          }
        } else if (!checked) {
          for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
            if (data.id === this.selfGroupMemberTableData[i].conId) {
              this.selfGroupMemberTableData.splice(i, 1);
            }
          }
        }
      },
      //删除自定义组成员
      addSelfGroupdeleteMember: function(id) {
        for (var i = 0; i < this.selfGroupMemberTableData.length; i++) {
          if (id === this.selfGroupMemberTableData[i].conId) {
            this.selfGroupMemberTableData.splice(i, 1);
            var node = this.$refs.SelfGroupTree.getNode(id);
            if (node !== null && node !== undefined) {
              node.checked = false;
            }

          }
        }
      },
      /**
       * 自定义组成员管理
       * @param item
       */
      handleSelfGroupMg(item) {
        //TODO 支持修改自定义组名称
        this.showManageBox = true;
        //保存自定义组id
        this.selfGroupId = item.id;
        let data = {
          groupId: item.id,
        };
        PersonalAddressBookManage.queryCustomizeGrpMem(data).then(res => {
          if (res.success) {
            if(res.obj.length>0){
              let resData = res.obj;
              for (let i=0;i<resData.length;i++){
                resData[i]['number'] = resData[i].extnumber;
                resData[i]['name'] = resData[i].name;

              }
                this.selfGroupMemberTableData = resData;
              console.log(this.selfMemberTableData);
            }


          }

        }).catch(err => {
          // 异常情况

        });

      },
      /**
       * 添加自定义组成员
       * **/
      handleAddCustomizeGrpMem() {
        //TODO 支持修改自定义组名称
        console.log('this.selfGroupMemberTableData');
        console.log(this.selfGroupMemberTableData);
        let objArray = [];
        for (let i = 0; i < this.selfGroupMemberTableData.length; i++) {
          let obj =
              {
                'groupId': this.selfGroupId,
                'subId': this.selfGroupMemberTableData[i].subId,
                'type': this.selfGroupMemberTableData[i].type,//0:终端 ；；1 调度员
              };
          objArray.push(obj)

        }
        let memberTableData = {
          memberTableData:objArray
        }
        PersonalAddressBookManage.addCustomizeGrpMem(memberTableData).then(res => {
          if (res.success) {
            this.$message.success('操作成功');
          }

        }).catch(err => {
          // 异常情况

        });
        this.showManageBox = false;

      },

      addPersonalAddressBook() {
        console.info('%c addPersonalAddressBook', 'color:green');
        var data = {};
        // data.getALLMemberTableData = this.getALLMemberTableData;
        // data.userId = this.userId;
        data.userId = this.UserAccount;
        let array = [];
        // data.userId = this.userId;
        var membersName = '';
        for (var i = 0; i < this.getALLMemberTableData.length; i++) {
          membersName += this.getALLMemberTableData[i].conName + ',';
          let obj = {
            type:this.getALLMemberTableData[i].type,
            conId:this.getALLMemberTableData[i].conId,
            conName:this.getALLMemberTableData[i].conName,
            number:this.getALLMemberTableData[i].number,
          }
          array.push(obj)
        }
        data.memberTableData = array

        membersName = membersName.substring(0, membersName.length - 1);
        var auditData = {
          dcId: this.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: this.$store.getters.userName + '添加个人通讯录:' + membersName,
          time: new Date,
          fleetId: this.$store.getters.scsFleetId,
          detailType: 27,
          remark: '',
        };

        PersonalAddressBookManage.addPersonalAddressBook(data).then(res => {
          if (res.success) {
            if (res.obj.count === 1) {
              this.GlobalFunc.GlaAddAudit(auditData);
              this.closeBox();
              this.$message.success('操作成功');
              this.query(1);

            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);
              this.$message('操作失败');
            }
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            this.$message('操作失败');
          }

        }).catch(err => {
          // 异常情况
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
          console.log(err);
        });
      },
      updatePersonalAddressBook() {
        var data = {
          id: this.form.id,
          conId: this.form.conId,
          type: this.form.type,
        };

        var auditData = {
          dcId: this.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: this.$store.getters.userName + '更新个人通讯录:',
          time: new Date,
          fleetId: this.$store.getters.scsFleetId,
          detailType: 27,
          remark: '',
        };
        // data.userId=DasConstant.userId;
        data.userId = this.UserAccount;
        PersonalAddressBookManage.updatePersonalAddressBook(data).then(res => {
          var res = res;
          if (res.success) {
            if (res.obj.count === 1) {
              this.GlobalFunc.GlaAddAudit(auditData);
              this.closeBox();
              this.$message.success('编辑成功');
              this.query();
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);
              this.$message('错误码' + res.obj.count);
            }
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            this.$message('内部错误');
          }

        }).catch(err => {
          // 异常情况
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
          console.log(err);
        });
      },
      closeBoxaddSelfGroupShowBox() {
        this.form.name = '';
        this.addSelfGroupShowBox = false;
      },
      /*添加*/
      addSelfGroupMember(formName) {
        let validate = false;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        let data = {
          userId: this.UserAccount,
          conId: 0,
          type: 4,
          remark: this.form.name,//自定义编组名字
        };
        //TODO
        PersonalAddressBookManage.addCustomizeGrp(data).then(res => {
          if (res.success) {
            if (res.obj.count == 1) {
              this.$message.success('添加成功');
              this.query(1);
              this.addSelfGroupShowBox = false;
            }
          }

        }).catch(err => {

        });
      },

      closeBox: function() {
        this.showBox = false;
      },
      operate(row, href) {
        if (href.indexOf('update') !== -1) {
          this.showBox = true;
          this.title = '编辑';
          // this.getMemberTableData();
        } else if (href.indexOf('details') !== -1) {
          console.log(row);
          this.DetailsDatas.name = row.name;
          this.DetailsDatas.extnum = row.extnum;
          this.DetailsDatas.typeName = row.zhognduanType;
          this.showDetails = true;
        } else {
          var auditData = {
            dcId: this.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: this.$store.getters.userName + '删除个人通讯录:' + row.name,
            time: new Date,
            fleetId: this.$store.getters.scsFleetId,
            detailType: 27,
            remark: '',
          };

          this.$confirm('是否确定删除', '', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            PersonalAddressBookManage.deletePersonalAddressBook({id: row.id}).then(res => {
              if (res.success) {
                this.$message.success('删除成功');
                this.GlobalFunc.GlaAddAudit(auditData);
                this.query();
              } else {

              }
            }).catch(err => {
              // 异常情况
              console.log(err);
              auditData.status = '异常';
              this.GlobalFunc.GlaAddAudit(auditData);
            });
          }).catch(() => {
          });

        }
      },
      getMemberTableData() {
        var data = {
          // conName: this.conName || '',
          userId: this.UserAccount,
          pageNum: 1,
          pageSize: 10000,
        };
        this.memberTableData = [];
        PersonalAddressBookManage.getPersonalAddressBook(data).then(res => {
          var data = res;
          if (res.success) {
            this.memberTableData = data.obj.list;
          } else {
          }

        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      changePageNum: function(val) {
        this.pageNum = val;
        this.query();
      }, changesAddPageNum: function(val) {
        this.addPage.pageNum = val;
      },
    },
  };
</script>

<style scoped>
    .fooltBtnL {
        margin-top: 100px;
    }

    .el-dialog__body {
        /*height: 30vh;*/
        height: 100%;
        overflow: auto;
    }

    /deep/ .el-icon-caret-right:before {
        font-size: 40px !important;
    }

    /deep/ .el-checkbox {
        /*height: 35px;*/
        /*width: 35px;*/
    }

    /deep/ .el-checkbox {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox__inner {
        width: 30px;
        height: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__input {
        vertical-align: middle !important;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:before {
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        line-height: 40px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:after {
        height: 12px;
        width: 12px;
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        /*line-height: 40px;*/
    }

    /deep/ .el-tree-node__content {
        height: 40px !important;
    }


    /deep/ .el-form-item__label {
        font-size: 22px !important;
    }

    /deep/ .el-tree-node {
        margin-top: 25px !important;
        height: auto;
    }

    /deep/ .el-input__inner {
        font-size: 20px;
    }

    /deep/ .el-dialog__title {
        font-size: 22px;
    }

    /deep/ .el-icon-close:before {
        font-size: 25px;
    }

    /*分页修改开始*/
    .el-pagination /deep/ .el-pagination__total {
        font-size: 20px;
        line-height: 60px;
    }

    .el-pagination /deep/ button /deep/ .el-icon .el-icon .el-icon-arrow-left {
        font-size: 50px;
        height: 60px;
        width: 40px;
    }

    /deep/ .el-pager li {
        margin-top: 15px;
        height: 60px;
        width: 60px;
        line-height: 60px;
        font-size: 18px;
    }

    /deep/ .el-pagination.is-background .btn-prev, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination.is-background .btn-next, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination .btn-prev .el-icon {
        font-size: 40px;
    }

    /deep/ .el-pagination .btn-next .el-icon {
        font-size: 40px;
    }

    /*分页修改结束*/

    #personalAddressBookManage .table-wrap {
        height: 800px !important;;
        /*overflow: auto;*/
    }

    @media screen and (max-width: 1920px) and (max-height: 1280px) {
        #personalAddressBookManage .table-wrap {
            height: 1200px !important;;
        }
    }

    .searchFormBtn {
        margin-left: 5px;
        display: inline-block;
    }

    .leftTreeBox {
        float: left;
        width: 60%;
        height: 300px
    }

    .memberTableData {
        float: right;
        width: 40%;
        height: 300px;
        background: ghostwhite;
        overflow: auto
    }

    .memberTableData-col {
        padding-top: 5px;
        font-size: 18px
    }

    .groupMgStyleBigBoxLeft {
        width: 40%;
        height: 450px;
        overflow: auto;
        display: inline-block;

    }

    .groupFromBox {
        margin-top: 50px;
    }

    .groupMgStyleBigBox {
        float: right;
        width: 60%;
        height: 450px;
        background: ghostwhite;
        overflow-y: auto;
        overflow-x: hidden;
    }
</style>
